<?php
/**
 * order CRUD class
 */
class order_model extends CI_Model {
    private $error;
    // default to be valid and see how optimized performance goes first
    private $is_valid_schema = true;
	/**
	 * use constructor to pre load database object
	 */
	public function __construct()
	{
		parent::__construct();
		$this->load->database();
	}

    /**
     * @param $type
     * @param $discount_rate
     * @param $tax_rate
     * @param $discount_amount
     * @param $subtotal
     * @param $tax
     * @param $total
     * @param $date
     * @param $time
     * @param $phone
     * @param $name
     * @param $address
     * @param $city
     * @param $state
     * @param $zipcode
     * @param $store_note
     * @param $customer_note
     * @param $order_food
     * @param int $id
     *
     * @return mixed
     */
    public function create($type, $discount_rate, $tax_rate, $discount_amount, $subtotal, $tax, $total, $date, $time
        ,$phone, $name, $address, $city, $state, $zipcode, $store_note, $customer_note, $order_food, $id=0)
    {
        $this->error = array('user' => array(), 'system' => array());
        if(true === $this->is_valid_schema)
        {
            //insert order info, and retrieve order id to be used in order food table
            // type, discount_rate, tax_rate, discount_amount, subtotal, tax, total, date, time, phone, name, address,
            // city, state, zipcode, store_note, customer_note
            $order_info = array(
                'type' => $type,
                'discount_rate' => $discount_rate,
                'tax_rate' => $tax_rate,
                'discount_amount' => $discount_amount,
                'subtotal' => $subtotal,
                'tax' => $tax,
                'total' => $total,
                'date' => $date,
                'time' => $time,
                'phone' => $phone,
                'name' => $name,
                'address' => $address,
                'city' => $city,
                'state' => $state,
                'zipcode' => $zipcode,
                'store_note' => $store_note,
                'customer_note' => $customer_note
            );

            if($id != 0){
                $order_info['id'] = $id;
            }
            $this->db->insert('order', $order_info);
            $order_id = $this->db->insert_id();

            //prepare data to be insert into order_food and order_food_option database
            $order_food_data = '';
            foreach($order_food as $o_f){
                $order_food_data .= '("' . $order_id . '", "' . $o_f['food_id'] . '", "' . $o_f['type'] . '", "' . $o_f['en_name'] . '", "' . $o_f['ch_name'] . '", "'
                    . $o_f['quantity'] . '", "' . $o_f['en_size'] . '", "' . $o_f['ch_size'] . '", "' . $o_f['price'] . '", "' . $o_f['sequence'] . '"), ';
            }
            // remove ',' at the end
            $order_food_data = substr($order_food_data, 0, -2);
            $order_food_sql = 'insert into order_food (order_id, food_id, type, en_name, ch_name, quantity, en_size, ch_size, price, sequence) values '
                . '' . $order_food_data;

            $this->db->query($order_food_sql);
        }
        return $this->error;
    }

    /**
     * retrieve order info by id
     * @param $order_id
     * @return array
     */
    public function read($order_id)
    {
        $this->db->where('id', $order_id);
        $order_info_query = $this->db->get('order');
        $order_info_result = $order_info_query->result_array();

        $this->db->where('order_id', $order_id);
        $order_food_info_query = $this->db->get('order_food');
        $order_food_info_result = $order_food_info_query->result_array();

        $order_array = array('info' => $order_info_result, 'food' => $order_food_info_result);
        return $order_array;
    }

    public function update($type, $discount_rate, $tax_rate, $discount_amount, $subtotal, $tax, $total, $date, $time
            ,$phone, $name, $address, $city, $state, $zipcode, $store_note, $customer_note, $order_food, $id)
    {
        $this->delete($id);
        $this->create($type, $discount_rate, $tax_rate, $discount_amount, $subtotal, $tax, $total, $date, $time
                ,$phone, $name, $address, $city, $state, $zipcode, $store_note, $customer_note, $order_food, $id);
    }

    public function delete($order_id)
    {
        $this->db->where('id', $order_id);
        $this->db->delete('order');
        $this->db->where('order_id', $order_id);
        $this->db->delete('order_food');
    }
}
